package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.common.MessageConst;
import com.itheima.common.RedisConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetMealService;
import com.itheima.utils.QiniuUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.UUID;

/**
* @Author : wangjie
* @CreateDate : 2020/3/31 14:49
* @Version : 1.0
* @Description : 检查套餐前端控制器
*/
@RestController
@RequestMapping("setmeal")
@Slf4j
public class SetMealController {
    //依赖注入
    @Reference
    private SetMealService setMealService;
    @Autowired
    private JedisPool jedisPool;

    /**
     * 检查套餐分页
     * @param pageBean
     * @return
     */
    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean pageBean){
        //调用service 方法
        PageResult pageResult = setMealService.queryPage(pageBean);
        return pageResult;
    }

    /**
     * 上传文件
     * @param imgFile
     * @return
     */
    @RequestMapping("upload")
    public Result upload(MultipartFile imgFile){
        try {
            //获取上传图片文件名
            String filename = imgFile.getOriginalFilename();
            //防止文件覆盖,对文件重命名
            String nowFileName= UUID.randomUUID().toString().replace("-","")+filename;
            //使用七牛云上传图片
            QiniuUtils.upload2QiNiu(imgFile.getBytes(),nowFileName);
            //添加一行 上传文件名存入redis中
            Jedis jedis = jedisPool.getResource();
            jedis.sadd(RedisConst.SETMEAL_PIC_RESOURCES,nowFileName);
            //上传路径返回给客户端
            String fileUrl = QiniuUtils.qiniu_img_url_pre + nowFileName;
            return new Result(true, MessageConst.PIC_UPLOAD_SUCCESS,fileUrl);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false,MessageConst.PIC_UPLOAD_FAIL);
        }
    }

    /**
     * 添加检查套餐
     * @param setmeal
     * @param checkgroupIds
     * @return
     */
    @RequestMapping("add")
    public Result add(@RequestBody Setmeal setmeal,Integer[] checkgroupIds){
        log.debug("add:{}",setmeal,checkgroupIds);
        try {
            //将存储在七牛云中的图片前端访问路径去掉
            setmeal.setImg(setmeal.getImg().replace(QiniuUtils.qiniu_img_url_pre,""));
            //调用service 方法 存储数据
            setMealService.add(setmeal,checkgroupIds);
            return new Result(true,MessageConst.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.ADD_SETMEAL_FAIL);
        }
    }
    @RequestMapping("deleteById")
    public Result deleteById(Integer id){
        try {
            setMealService.deleteById(id);
            return new Result(true,"删除检查套餐成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除检查套餐失败");
        }
    }
}
